package com.aaa.service.impl;

import com.aaa.dao.DictTypeDao;
import com.aaa.dao.UserDao;
import com.aaa.entity.Dept;
import com.aaa.entity.DictData;
import com.aaa.entity.DictType;
import com.aaa.entity.User;
import com.aaa.service.DictTypeService;
import com.aaa.vo.DictTypeVo;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.Objects;
@Service
public class DictTypeServiceImpl extends ServiceImpl<DictTypeDao, DictType> implements DictTypeService {

    @Autowired
    public DictTypeDao dictTypeDao;
    @Override
    public Result<IPage<DictType>> findByConditions(Integer currentPage, Integer pageSize, DictTypeVo dictTypeVo) {
        IPage<DictType> page=new Page<>(currentPage,pageSize);
        QueryWrapper<DictType> wrapper=new QueryWrapper<>();
        if(StringUtils.hasText(dictTypeVo.getDictName())){
            wrapper.like("dict_name",dictTypeVo.getDictName());
        }
        if(StringUtils.hasText(dictTypeVo.getDictType())){
            wrapper.eq("dict_type",dictTypeVo.getDictType());
        }
        if(Objects.nonNull(dictTypeVo.getStatus())){
            wrapper.eq("status",dictTypeVo.getStatus());
        }
        if(Objects.nonNull(dictTypeVo.getDateRange())&&dictTypeVo.getDateRange().length==2){
            wrapper.between("create_time",dictTypeVo.getDateRange()[0],dictTypeVo.getDateRange()[1]);
        }

        IPage<DictType> page1 = dictTypeDao.selectPage(page, wrapper);
        return new Result<IPage<DictType>>(2000,"查询用户成功",page1);
    }
    @Override
    public boolean saveAndUpdate(DictType dictType) {
        int i=-1;
        if (dictType.getCreateTime()==null){
            dictType.setCreateTime(new Date());
            dictType.setUpdateTime(new Date());
        }else {
            dictType.setUpdateTime(new Date());
        }
        if(dictType.getDictId()==null){
            i=dictTypeDao.insert(dictType);
            System.out.println("====="+dictType.getStatus());
        }else {
            i=dictTypeDao.updateById(dictType);
        }
        if (i>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean delById(Long id) {
        int i = dictTypeDao.deleteById(id);
        if (i > 0) {
            return true;
        }
        return false;
    }
}
